package com.example.manpower.Server.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manpower.Entity.Admin;
import com.example.manpower.Mapper.AdminMapper;
import com.example.manpower.Server.AdminServer;
import com.example.manpower.Util.RedisUtil;
import com.example.manpower.Util.Result;
import com.example.manpower.Util.TokenUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;

@Service
public class AdminServerImpl implements AdminServer {


    @Autowired
    AdminMapper adminMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        admin.setAdminPassword(SecureUtil.md5(admin.getAdminPassword()));
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_account",admin.getAdminAccount());
        queryWrapper.eq("admin_password",admin.getAdminPassword());
        Admin admin1 = adminMapper.selectOne(queryWrapper);
        if (admin1 != null) {
            TokenUtil tokenUtil = new TokenUtil();
            String token = tokenUtil.createToken(admin1);

            Gson gson = new Gson();
            String adminjson =gson.toJson(admin1);
            redisUtil.setWithExpire(token,adminjson,100000);
            System.out.println("登录成功");
            return new Result(200,"token生成成功",token);
        }

        return new Result(400,"token生成失败",null);
    }

    @Override
    public Result add(Admin admin) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_account",admin.getAdminAccount());
        Admin admin1 = adminMapper.selectOne(queryWrapper);
        if (admin1 != null) {
            return new Result(400,"该管理员已经存在",null);
        }
        admin.setAdminPassword(SecureUtil.md5(admin.getAdminPassword()));
        int num = adminMapper.insert(admin);
        return new Result(200,"添加管理员成功",num);
    }

    //分页查询
    @Override
    public Result selectList(Admin admin) {
//        Page page = new Page(user.getPage(),user.getSize());
        Page page = new Page(admin.getPage(),admin.getSize());
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if (admin.getAdminName() != null) {
            queryWrapper.like("admin_name",admin.getAdminName());
        }
        adminMapper.selectPage(page, queryWrapper);
        List<Admin> list = page.getRecords();
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("list",list);
        map.put("total",page.getTotal());
        return new Result(200,"查询成功",map);
    }

    //删除
    @Override
    public Result delete(Admin admin) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_id",admin.getAdminId());
        int num = adminMapper.delete(queryWrapper);
        if (num != 0) {
            return new Result(200,"删除成功",num);
        }
        return new Result(400,"删除失败",null);
    }

    @Override
    public Result update(Admin admin) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_id",admin.getAdminId());
        int num = adminMapper.update(admin, queryWrapper);
        return new Result(200,"修改成功",num);
    }


}

